package edu.scau.web.pos;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.core.util.ServletUtils;
import edu.scau.pos.domain.Category;
import edu.scau.pos.service.CategoryService;
import edu.scau.pos.vo.CategoryVo;
import edu.scau.system.domain.LoginUser;
import edu.scau.system.service.TokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/category")
@Tag(name = "分类管理", description = "分类管理相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @Operation(
            summary = "查找分类",
            description = "查找所有未删除的分类信息,访问权限:ALL",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = CategoryVo.class))),
                    @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = CategoryVo.class)))
            }
    )
    @GetMapping("/listAll")
    public AjaxResult listALL(){
       List<CategoryVo> categoryList=categoryService.listAll();
       return AjaxResult.success(categoryList);
    }

    @Operation(
            summary = "分页查找分类",
            description = "分页查找所有未删除的分类信息,默认查找无父分类(0)的数据,访问权限ALL",
            parameters = {
                    @Parameter(name = "pageNum", description = "页码", required = true,schema = @Schema(type = "int")),
                    @Parameter(name = "pageSize", description = "每页数据条数", required = true,schema = @Schema(type = "int")),
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = CategoryVo.class))), //  返回 PageInfo 对象
                    @ApiResponse(responseCode = "500", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))) //  返回 AjaxResult 对象
            }
    )
    @GetMapping("/listByPage")
    public AjaxResult listByPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<CategoryVo> categoryList=categoryService.listAll();
        PageInfo<CategoryVo> pageInfo = new PageInfo(categoryList);
        return AjaxResult.success(pageInfo);
    }


    @Operation(
            summary = "根据ID查找分类",
            description = "根据分类ID查找分类信息,访问权限:ALL",
            parameters = {
                    @Parameter(name = "categoryId", description = "分类ID", required = true,schema = @Schema(type = "Long"))
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = CategoryVo.class))),
                    @ApiResponse(responseCode = "204", description = "分类不存在", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @GetMapping("/findCategoryById")
    public AjaxResult findCategoryById(@RequestParam("categoryId") Long categoryId){
        CategoryVo category=categoryService.findCategoryById(categoryId);
        if(category != null) {
            return AjaxResult.success(category);
        }
        else {
            return AjaxResult.success("分类不存在或者已删除！",HttpStatus.NO_CONTENT);
        }
    }


    @Operation(
            summary = "更新分类",
            description = "根据ID更新分类信息,id是必须的,访问权限:cashier、admin",
            requestBody = @RequestBody(
                    description = "更新分类条件",
                    required = true, //  查询条件可以为空
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = CategoryVo.class)
                    )
            ),
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "500", description = "更新失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @PreAuthorize("hasAnyAuthority('cashier','admin')")
    @PutMapping("/update")
    public AjaxResult updateCategory(@Validated  @org.springframework.web.bind.annotation.RequestBody CategoryVo category ){
        int res=categoryService.updateCategory(category);
        return res>0 ? AjaxResult.success(HttpStatus.SUCCESS):AjaxResult.error("更新失败");
    }

    @Operation(
            summary = "插入分类",
            description = "插入分类信息,sn是必要字段,访问权限:cashier、admin",
            requestBody = @RequestBody(
                    description = "插入分类条件",
                    required = true, //  查询条件可以为空
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = CategoryVo.class)
                    )
            ),
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "500", description = "插入失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @PreAuthorize("hasAnyAuthority('cashier','admin')")
    @PutMapping("/insert")
    public AjaxResult insertCategory(@Validated @org.springframework.web.bind.annotation.RequestBody CategoryVo categoryVo){
        int res=categoryService.insertCategory(categoryVo);
        return res>0? AjaxResult.success(200):AjaxResult.error("插入失败！");
    }

    @Operation(
            summary = "根据ID删除分类",
            description = "根据分类ID删除分类信息,访问权限:cashier、admin",
            parameters = {
                    @Parameter(name = "categoryId", description = "分类ID", required = true,schema =@Schema(type = "Long"))
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "500", description = "删除失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @PreAuthorize("hasAnyAuthority('cashier','admin')")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("categoryId") Long categoryId){
        int res=categoryService.deleteCategoryById(categoryId);
        return res>0 ? AjaxResult.success("删除成功",200):AjaxResult.error("删除失败");
    }
}
